package SetInterfaceTest;
import org.junit.Test;

import java.util.*;

/**
 *
 * @author 李泽伟
 */
public class MainTest {
    @Test
    public void test(){
        HashSet set = new HashSet();
        set.add(123);
        set.add(23);
        set.add("CC");
        set.add("dd");
        //[CC, dd, 23, 123]
        System.out.println(set);
        set.add(new User("lzj", 18));
        set.add(new User("lzj", 18));
        // [CC, dd, User{name='lzj', age=18}, 23, 123] 不可重复的
        System.out.println(set);
    }
    @Test
    public void test1(){
        LinkedHashSet linkedHashSet = new LinkedHashSet();
        linkedHashSet.add(123);
        linkedHashSet.add("adad");
        linkedHashSet.add(234);
        linkedHashSet.add("sd");
        linkedHashSet.add("sd");
        linkedHashSet.add("adada");
        //[123, adad, 234, sd, adada]
        System.out.println(linkedHashSet);
        for (Object obj:linkedHashSet){
            System.out.println(obj);
        }
    }
    @Test
    public void test2(){
        Comparator com = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof User && o2 instanceof User){
                    User u1 = (User) o1;
                    User u2 = (User) o2;
                    return Integer.compare(u1.getAge(), u2.getAge());
                }else {
                    throw new RuntimeException("参数不匹配");
                }
            }
        };
        TreeSet treeSet = new TreeSet(com);
        treeSet.add(new User("lzj", 31));
        treeSet.add(new User("jtq", 21));
        treeSet.add(new User("hh", 13));
        treeSet.add(new User("hh", 23));
        Iterator iterator = treeSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
class User implements Comparable{
    private String name;
    private int age;
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        User user = (User) o;
        return age == user.age && name.equals(user.name);
    }
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    @Override
    public int compareTo(Object o) {
        System.out.println("run compareTo");
        if(o instanceof User){
            User user = (User) o;
            return this.name.compareTo(user.name);
        }else {
            throw new RuntimeException("输入的类型有误");
        }
    }
}












